package com.gzc.pm.logic
{
	import cn.skyclass.business.SessionManager;
	import cn.skyclass.events.LoadEvent;
	
	import com.gzc.pm.bean.DetectioncardBean;
	import com.gzc.pm.bean.DetsetbBean;
	import com.gzc.pm.bean.LineChartItemBean;
	import com.gzc.pm.bean.ProcessRouteBean;
	import com.gzc.pm.util.PopUpUtil;
	
	import mx.collections.ArrayCollection;
	
	/**
	 * （除了当前页面显示的工序检测项）检测数据背景分析
	 */
	public class CollectionsAnalyser
	{
		public function CollectionsAnalyser(procIdExcepted:int, startDate:Date, standard:int)
		{
			_procIdExcepted = procIdExcepted;
			_startDate = startDate;
			_standard = standard;
			loadProcess();//加载需要背景监测的工序
		}
		
		private var _procIdExcepted:int;
		private var _startDate:Date;
		private var _standard:int;
		private var _procArray:Array;
		
		protected function loadProcess():void{
			var procEquData:ProcEquLoader = SessionManager.current.getAttribute('procEquData') as ProcEquLoader;
			_procArray = [];
			for each(var proc:ProcessRouteBean in procEquData.procList){
				if(proc.prId != _procIdExcepted){
					_procArray.push(proc);
				}
			}
		}
		
		public function analyse():void{
			var procLength:uint = _procArray.length;//背景监测的工序数量
			if(procLength==0) return;
			
			var alarmProcList:ArrayCollection = new ArrayCollection();//告警的工序
			var analysedCount:uint = 0;//已分析的工序数量
			
			var loader:DetectionCardLoader;
			var proc:ProcessRouteBean;
			loadCollections();
			
			function loadCollections(procIndex:uint=0):void{
				proc = ProcessRouteBean(_procArray[procIndex]);
				loader = new DetectionCardLoader(proc.prId, _startDate);
				loader.addEventListener(LoadEvent.LOADED, onLoaded);
				loader.load();
			}
			
			function onLoaded(e:LoadEvent):void{
				loader.removeEventListener(LoadEvent.LOADED, onLoaded);
				//分析数据
				var alarmInProc:Boolean = false;
				for each(var detsetb:DetsetbBean in loader.detsetbs){
					var parser:CollectionsParser = new CollectionsParser(loader.resultList, detsetb, _standard);
					var ds:ArrayCollection = parser.parse();
					for each(var chartItem:LineChartItemBean in ds){
						if(chartItem.b1 || chartItem.b2 || chartItem.b3 || chartItem.b4 ||
							chartItem.b5 || chartItem.b6 || chartItem.b7 || chartItem.b8){
							alarmProcList.addItem({procId:proc.prId, procName:proc.prName});
							alarmInProc = true;
							break;
						}
					}
					if(alarmInProc) break;
				}
				analysedCount++;
				if(analysedCount < procLength){//加载下一个工序的检测数据
					loadCollections(analysedCount);
				}else if(alarmProcList.length>0){//所有工序检测完毕，且出现告警
					PopUpUtil.showBackgroundAlarmInfo(alarmProcList);
				}
			}
		}
		
	}
}